<?php

namespace website\controllers;

use common\models\Recharge;
use common\server\recharge\RechargeServer;
use Yii;
use common\server\recharge\AlipayServer;
use common\models\EmploymentRecharge;

/**
 * 第三方回调
 * Displays third platform notify.
 */
class NotifyController extends Controller {

    // 不需要验证CSRF值
    public $enableCsrfValidation = false;

    /**
     * 支付宝充值回调
     * @return string
     */
    public function actionAlipay()
    {
        $params = $this->request->post();

//        $params = [
//            'gmt_create' => '2018-05-07 19:11:28',
//            'charset' => 'UTF-8',
//            'gmt_payment' => '2018-05-07 19:11:38',
//            'notify_time' => '2018-05-07 19:11:38',
//            'subject' => '订单支付',
//            'sign' => 'gycj6VfUl08Ysb/IpNgvbLNZlAAd7Tc4YnoNkm5ze8YK0iNE9ipsgVfEif71ULlvfkzwahK0ghuvCGnBjYrujxFlgRVgsqiPnAkhNwOthBYF0iNpAQlHxscHSVB9sDB57z8K69KUtFSaALBWEY39VUDmf2kTDQHc+P5VOuiOS/AjDSF/zvifWniXO/nfA7x+iPVN+aNQx6bHAH8Ph2PC5nTexkINcCSU9AT4myVz2sVCDfg3p92AKAm7FiSnpHftcEfOC4or3QHevxtYnz+ttG4WIKNvMo5XBZ2caI2TbrD3JI8OW2RjREdiw4/XVdR0sQiSgbnxm16IrX+GUrodZg==',
//            'buyer_id' => '2088612985245000',
//            'invoice_amount' => '0.01',
//            'version' => '1.0',
//            'notify_id' => '974802d117cc48cb0667092648f6098g05',
//            'fund_bill_list' => '[{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]',
//            'notify_type' => 'trade_status_sync',
//            'out_trade_no' => 'UBOUEKUZ97NI84GC4',
//            'total_amount' => '0.01',
//            'trade_status' => 'TRADE_SUCCESS',
//            'trade_no' => '2018050721001004000552341270',
//            'auth_app_id' => '2018031802399008',
//            'receipt_amount' => '0.01',
//            'point_amount' => '0.00',
//            'app_id' => '2018031802399008',
//            'buyer_pay_amount' => '0.01',
//            'sign_type' => 'RSA2',
//            'seller_id' => '2088031351277162',
//        ];
        file_put_contents('alipay_notify.txt', '['.date('Y-m-d H:i:s').']'.var_export($this->request->post(), true));
        Yii::info('支付宝回调：'.json_encode($params));
        if(empty($params)) {
            return 'success';
        }
        $server = new RechargeServer(Recharge::PlatformAlipay);
        // 验证成功
        if($server->baseServer->baseApi->alipayApi->check($params)) {
            // 转换订单金额单位
            $params[$server->baseServer->amountKey] = bcmul($params[$server->baseServer->amountKey], 100);
            if($server->notify($params)) {
                return 'success';
            }
        }
        return 'failed';
    }

    /**
     * 微信充值回调
     * @return string
     */
    public function actionWechat()
    {
        return 'success';
    }
}